package com.nbgroup.web.user;

import com.nbgroup.dao.UserDao;
import com.nbgroup.entity.Role;
import com.nbgroup.entity.RoleUser;
import com.nbgroup.entity.User;
import com.nbgroup.service.RoleService;
import com.nbgroup.service.user.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Hanh on 2018/8/16 0016.
 * 用户信息管理 数据处理
 */
@Controller
@RequestMapping("/user")
public class UserTurnController {
    @Autowired
    UsersService usersService;
    @Autowired
    UserDao userserDao;
    @Autowired
    RoleService roleService;

    //列表(跳转)
    @RequestMapping(value = "/toUserPage", method = RequestMethod.GET)
    public String userPage(Map<String, Object> map,User user, Integer page, Integer pageSize,Principal principal) throws Exception {
        pageSize = 8;
        Page<User> userPage = usersService.findByPage(user,page,pageSize);
        map.put("datas", userPage);
        User userObject = userserDao.findByUsernameAndSts(principal.getName(), "1");
        map.put("user", userObject);
        Page<Role> byPage = roleService.findByPage(1, 100);
        map.put("role",byPage.getContent());
        return "user/user_list";
    }
    //跳转到添加
    @RequestMapping(value = "/toUserAdd", method = RequestMethod.GET)
    public String toUserAdd(Map<String, Object> map, Principal principal) throws Exception {
        return "user/user_insert";
    }
    //跳转到修改
    @RequestMapping(value = "/toUserUpdate/{id}", method = RequestMethod.GET)
    public String toUserUpdate(Map<String, Object> map, Principal principal,@PathVariable("id") String id) throws Exception {
        User user = usersService.getInfoById(id);
        map.put("user",user);
        return "user/user_update";
    }
    //添加
    @ResponseBody
    @RequestMapping(value = "/userOnAdd", method = RequestMethod.POST)
    public Map<String,Object> userOnAdd(User user){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("Message",usersService.save(user));
        return map;
    }
    //修改
    @ResponseBody
    @RequestMapping(value = "/userOnUpdate", method = RequestMethod.POST)
    public Map<String,Object> userOnUpdate(User user){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("Message",usersService.update(user));
        return map;
    }
    //删除
    @ResponseBody
    @RequestMapping(value = "/userOnDelete", method = RequestMethod.POST)
    public Map<String,Object> userOnDelete(String id){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("Message",usersService.delete(id));
        return map;
    }
    //重置
    @ResponseBody
    @RequestMapping(value = "/userOnReset", method = RequestMethod.POST)
    public Map<String,Object> userOnReset(String id){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("Message",usersService.reset(id));
        return map;
    }


    //根据id获取信息
    @ResponseBody
    @RequestMapping(value ="/getDateById/{id}",method = RequestMethod.GET)
    public Map<String,Object> getDateById(@PathVariable("id") String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        User user = usersService.getInfoById(id);
        map.put("user",user);
        RoleUser roleByUserId = roleService.findRoleByUserId(user.getId() + "");
        List<Role> role =new ArrayList<>();
        role.add(roleService.findOne(roleByUserId.getRoleId()));
        role.addAll(roleService.findRoleNotBy(roleByUserId.getRoleId()));
        map.put("roleId",role);
        return map;
    }
}
